﻿
using l1cs.server.model.Instance;
using l1cs.server.templates;
using System.Threading;
namespace l1cs.server.model
{
    public class L1ItemDelay
    {
        private static Logger _log = Logger.getLogger( typeof( L1ItemDelay ).FullName );

        //private L1ItemDelay()
        //{
        //}

        //static public class ItemDelayTimer {
        private int delayId;
        private int delayTime;
        private L1PcInstance pc;

        public L1ItemDelay( L1PcInstance cha, int id, int time )
        {
            pc = cha;
            delayId = id;
            delayTime = time;
        }

        public void L1ItemDelayTimerSub( object state )
        {
            stopDelayTimer( delayId );
        }

        public void stopDelayTimer( int delayId )
        {
            pc.removeItemDelay( delayId );
        }
        //}

        public void onItemUse( ClientThread client, L1ItemInstance item )
        {
            pc = client.getActiveChar();

            if ( item.getItem().getType2() == 0 )
            {
                // 種別：その他のアイテム
                delayId = ( (L1EtcItem)item.getItem() ).get_delayid();
                delayTime = ( (L1EtcItem)item.getItem() ).get_delaytime();
            }
            else if ( item.getItem().getType2() == 1 )
            {
                // 種別：武器
                return;
            }
            else if ( item.getItem().getType2() == 2 )
            {
                // 種別：防具

                if ( item.getItem().getItemId() == 20077
                        || item.getItem().getItemId() == 20062
                        || item.getItem().getItemId() == 120077 )
                {
                    // インビジビリティクローク、バルログブラッディクローク
                    if ( item.isEquipped() && !pc.isInvisble() )
                    {
                        pc.beginInvisTimer();
                    }
                }
                else
                {
                    return;
                }
            }

            // TODO 優先度9 タイマースレッド
            //ItemDelayTimer timer = new ItemDelayTimer( pc, delayId, delayTime );
            pc.addItemDelay( delayId, this );
            //GeneralThreadPool.getInstance().schedule( timer, delayTime );
            Timer timer = new Timer( new TimerCallback( L1ItemDelayTimerSub ), null, 1000, Timeout.Infinite );            
        }
    }
}
